package grid
{
	public class Topology
	{
		import mx.controls.Alert;
		
		public function Topology()
		{
		}
		var previousCoords = new Array();
		public function placeUnitOnGrid(selectedUnit,coords,rotation=false)
		{
			switch (selectedUnit)
			{
				case "wraith":
					if(!intersects(selectedUnit,coords))
					{
						previousCoords.push([{X:coords.X,Y:coords.Y},{X:coords.X,Y:coords.Y+1},{X:coords.X,Y:coords.Y+2}]);
						return [{X:coords.X,Y:coords.Y},
								{X:coords.X,Y:coords.Y+1},
								{X:coords.X,Y:coords.Y+2}];
					}
					else
					{Alert.show("You cannot place this Unit here.");return false;}
					break;
				case "hammer":
					if(!intersects(selectedUnit,coords))
					{ 
						previousCoords.push([{X:coords.X,Y:coords.Y},
							{X:coords.X,Y:coords.Y+1},
							{X:coords.X+1,Y:coords.Y},
							{X:coords.X,Y:coords.Y+1}]);
						return [{X:coords.X,Y:coords.Y},
								{X:coords.X,Y:coords.Y+1},
								{X:coords.X+1,Y:coords.Y},
								{X:coords.X+1,Y:coords.Y+1}];
					}
					else
					{Alert.show("You cannot place this Unit here.");return false;}
					break;
				case "destroyer":
					if(!intersects(selectedUnit,coords))
					{
						previousCoords.push([{X:coords.X,Y:coords.Y},{X:coords.X,Y:coords.Y+1},{X:coords.X,Y:coords.Y+2},{X:coords.X-1,Y:coords.Y+1},{X:coords.X+1,Y:coords.Y+1}]);
						return [{X:coords.X,Y:coords.Y},
							{X:coords.X,Y:coords.Y+1},
							{X:coords.X,Y:coords.Y+2},
							{X:coords.X-1,Y:coords.Y+1},
							{X:coords.X+1,Y:coords.Y+1}];
					}
					else
					{Alert.show("You cannot place this Unit here.");return false;}
					break;
				case "killBike":
					if(!intersects(selectedUnit,coords))
					{
						previousCoords.push([{X:coords.X,Y:coords.Y},{X:coords.X+1,Y:coords.Y+1}]);
						return [{X:coords.X,Y:coords.Y},
								{X:coords.X+1,Y:coords.Y+1}];
					}
					else
					{Alert.show("You cannot place this Unit here.");return false;}
					break;
				case "oculus":
					if(!intersects(selectedUnit,coords))
					{
						previousCoords.push([{X:coords.X,Y:coords.Y},{X:coords.X,Y:coords.Y+1},{X:coords.X-1,Y:coords.Y},{X:coords.X+1,Y:coords.Y}]);
						return [
							{X:coords.X,Y:coords.Y},
							{X:coords.X,Y:coords.Y+1},
							{X:coords.X-1,Y:coords.Y},
							{X:coords.X+1,Y:coords.Y}];
					}
					else
					{Alert.show("You cannot place this Unit here.");return false;}
			}
		}
		
		public function intersects(selectedUnit,coords,aiPos=false)
		{
			if(aiPos!=false)
			{previousCoords = aiPos;}
			var intersect = false;
			for each(var o in previousCoords)
			{if(intersect==true)break;
				for each(var p in o)
				{if(intersect==true)break;
					switch(selectedUnit)
					{
						case "wraith":
							var exists = (p.Y!=coords.Y||p.Y!=coords.Y+1||p.Y!=coords.Y+2);
							var exists1 = p.X!=coords.X;
							if((exists==true||exists1==true)&&coords.Y<=8&&coords.X>=1&&coords.Y>=1) 
							{}
							else 
							{intersect =  true;}
							break;
						case "hammer":
							var exists1 = p.X!=coords.X||p.Y!=coords.Y;
							var exists2 = p.X!=coords.X+1||p.Y!=coords.Y;
							var exists3 = p.X!=coords.X+1||p.Y!=coords.Y+1;
							var exists4 = p.X!=coords.X||p.Y!=coords.Y+1;
							if((exists1==true&&
								exists2==true&&
								exists3==true&&
								exists4==true)&&coords.X<10&&coords.Y<10&&coords.X>=1&&coords.Y>=1)
							{}
							else
								intersect = true;
							break;
						case "killBike":
							var exists1 = p.X!=coords.X||p.Y!=coords.Y;
							var exists2 = p.X!=coords.X+1||p.Y!=coords.Y+1;
							if((exists1==true&&
							   exists2==true)&&coords.X<10&&coords.Y<10&&coords.X>=1&&coords.Y>=1)
							{}
							else
								intersect =  true;
							break;
						case "oculus":
							var exists1 = p.X!=coords.X||p.Y!=coords.Y;
							var exists2 = p.X!=coords.X||p.Y!=coords.Y+1;
							var exists3 = p.X!=coords.X-1||p.Y!=coords.Y;
							var exists4 = p.X!=coords.X+1||p.Y!=coords.Y;
							if((exists1==true&&
								exists2==true&&
								exists3==true&&
								exists4==true)&&coords.X<10&&coords.Y<10&&coords.X>1&&coords.Y>0)
							{}
							else
								intersect = true;
							break;
						case "destroyer":
							var exists1 = p.X!=coords.X||p.Y!=coords.Y; 
							var exists2 = p.X!=coords.X||p.Y!=coords.Y+1;
							var exists3 = p.X!=coords.X||p.Y!=coords.Y+2;
							var exists4 = p.X!=coords.X-1||p.Y!=coords.Y+1;
	 						var exists5 = p.X!=coords.X+1||p.Y!=coords.Y+1;
							if((exists1==true&&
								exists2==true&&
								exists3==true&&
								exists4==true&&
								exists5==true)&&coords.X<=9&&coords.Y<=8&&coords.X>1&&coords.Y>0)
							{}
							else
								intersect = true;
							break;
					}
				}
				
			}
			if(previousCoords.length==0)
			{
				if(selectedUnit=="wraith"&&coords.Y>8)intersect = true;
				else if((selectedUnit=="hammer")&&(coords.X>9||coords.Y>9))intersect = true;
				else if((selectedUnit=="oculus")&&(coords.X>9||coords.Y>9||coords.X<2||coords.Y<1))intersect = true;
				else if((selectedUnit=="destroyer")&&(coords.X>9||coords.Y>8||coords.X<2||coords.Y<1))intersect = true;
				else if((selectedUnit=="killBike")&&(coords.X>9||coords.Y>9)) intersect = true;
			}
			return intersect;
		}
		
		// COORDINATE PRODUCTION.............. FROM GRID...........
		public var coords = {X:0,Y:0}; 
		public function coordsToPos(num)
		{
			var n:Number;
			if (Number(String(num).slice(-2))>=10)
			{n=Number(String(num).slice(-2));}
			else
			{
				n=Number(String(num).slice(-1));
			}
			
			var n2:Number;
			if (Number(String(num).slice(-2))>=10)
			{n2=Number(String(num).slice(0,-3));}
			else
			{
				n2=Number(String(num).slice(0,-2));
			}
			coords = {X:n2,Y:n};
			return {X:n2,Y:n};
		}
		
		// GET MAX HIT OPTIONS
		// N:  X-2,	Y
		// NE: X-1,	Y+1
		// E:  X,  	Y+2
		// SE: X+1,	Y+1
		// S:  X+2,	Y
		// SW: X+1,	Y-1
		// W:  X,  	Y-2
		// NW: X-1, Y-1
		public function getMaxOptions(hit)
		{
			var MAXx = hit.X+2;
			var MINx = hit.X-2;
			var MAXy = hit.Y;
			var MINy = hit.Y;
			
			var options = new Array();
			options = [
				{X:hit.X,  Y:hit.Y+1},// E
				{X:hit.X,  Y:hit.Y-1},// W
				{X:hit.X-1,Y:hit.Y},  // N
				{X:hit.X+1,Y:hit.Y},  // S
				{X:hit.X-1,Y:hit.Y+1},//diagonal up right
				{X:hit.X+1,Y:hit.Y+1},//diagonal down right
				{X:hit.X+1,Y:hit.Y-1},//diagonal down left
				{X:hit.X-1,Y:hit.Y-1},//diagonal up left
				{X:MAXx,Y:MAXy}, //Extremes up
				{X:MINx,Y:MINy}, //Extremes bot
				{X:hit.X,  Y:hit.Y+2},//Extremes right
				{X:hit.X,  Y:hit.Y-2}]//Extremes left
			
				return options;
		}
		
		public function getZones()
		{
			
		}
	}
}